package com.icehand.canal.config;

import com.icehand.canal.client.CanalClient;
import com.icehand.canal.client.SimpleCanalClient;
import com.icehand.canal.client.transfer.MessageTransponderBuilder;
import com.icehand.canal.handler.HandlerWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

/**
 * @author icehand
 */
@Slf4j
public class CanalClientConfiguration {
    @Autowired
    private CanalConfig canalConfig;

    @Bean
    @ConditionalOnClass(value = {com.icehand.canal.handler.HandlerWrapper.class})
    @ConditionalOnMissingBean
    private CanalClient canalClient(HandlerWrapper wrapper){
        log.info("init canal client");
        CanalClient canalClient = new SimpleCanalClient(canalConfig, MessageTransponderBuilder.defaultTransponderFactory(),wrapper);
        canalClient.start();
        return canalClient;
    }
}
